package cj.com.chabaike.ui.view;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.List;
import java.util.logging.Logger;

import cj.com.chabaike.R;
import cj.com.chabaike.bll.RWServer;
import cj.com.chabaike.entity.HomeData;
import cj.com.chabaike.ui.listener.ABaseLinearLayoutManager;
import cj.com.chabaike.ui.listener.OnRecyclerViewScrollListener;
import cj.com.chabaike.ui.listener.OnRecyclerViewScrollLocationListener;
import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;

/**
 * Created by CJLAB on 2015/8/13.
 */
public class RecyclerLoaderFooter  {
    Context context;
    View footerView;
    RWServer rws;
    Button loadBtn;
    LinearLayout loadLayout;
    public RecyclerLoaderFooter(Context context) {

        this.context = context;
        this.rws = RWServer.getInstance(context);
    }

    public View getFootView(RecyclerView recyclerView, final WaveSwipeRefreshLayout mWaveSwipeRefreshLayout, final IFooterBtndown ifb) {
         footerView = LayoutInflater.from(context).inflate(R.layout.recycler_list_loadbottom, null);
        loadBtn = (Button) footerView.findViewById(R.id.foot_load_btn);

        loadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ifb.whileFootBtnDown();
            }
        });



        loadLayout = (LinearLayout) footerView.findViewById(R.id.foot_load_layout);
//        不知道为什么在xml设置的“android:layout_width="match_parent"”无效了，需要在这里重新设置
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footerView.setLayoutParams(lp);

        recyclerView.setHasFixedSize(true);

        final ABaseLinearLayoutManager layoutManager = new ABaseLinearLayoutManager(context);
        layoutManager.setOnRecyclerViewScrollLocationListener(recyclerView, new OnRecyclerViewScrollLocationListener() {
            @Override
            public void onTopWhenScrollIdle(RecyclerView recyclerView) {
                Log.d("RecyclerLoaderFooter", "onTopWhenScrollIdle...");
                mWaveSwipeRefreshLayout.setisPullDown(true);
            }

            @Override
            public void onBottomWhenScrollIdle(RecyclerView recyclerView) {
                Log.d("RecyclerLoaderFooter", "onBottomWhenScrollIdle...");
                footerView.setVisibility(View.VISIBLE);

                /*
                ThreadPool.go(new Runtask<Object, Object>() {
                    @Override
                    public Object runInBackground() {
                        ABThreadUtil.sleep(3000);
                        return null;
                    }

                    @Override
                    public void onResult(Object result) {
                        super.onResult(result);
                        refreshLayout.setRefreshing(false);
                        footerView.setVisibility(View.GONE);
                    }
                });*/
            }
        });
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.getRecyclerViewScrollManager().addScrollListener(recyclerView, new OnRecyclerViewScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                Log.e("RecyclerListener",""+newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                //Log.e("RecyclerListener","x="+ dx + "y="+dy);
               // mWaveSwipeRefreshLayout.setisPullDown(false);
            }
        });
        recyclerView.setLayoutManager(layoutManager);

        // initData();
        return  footerView;
    }



    public  void FooterBtnVisable(boolean FooterBtnVisable){
        if(FooterBtnVisable){
            loadBtn.setVisibility(View.VISIBLE);
            loadLayout.setVisibility(View.INVISIBLE);
        }else {
            loadLayout.setVisibility(View.VISIBLE);
            loadBtn.setVisibility(View.INVISIBLE);
        }



    }


    public  interface  IFooterBtndown {
        void whileFootBtnDown();

    }

}
